/*
 * This file is part of quickLoad, a software for gamers
 *
 * Copyright (C) 2005-2008 Lucas Romero
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using ICSharpCode.SharpZipLib.Zip;
using Encoder=System.Drawing.Imaging.Encoder;

namespace QuickLoad.Controls
{
    
    public partial class PreviewToolTip : UserControl
    {
        delegate void AddItemCallback(StringCollection AddingItems, string Map);
        public delegate void ImageClickedEventHandler(object sender, ImageClickedEventArgs e);
        public event ImageClickedEventHandler ImageClicked;
        string m_Path = string.Empty;
        Thread DemoParser = null;
        public PreviewToolTip(Form parent)
        {
            InitializeComponent();
            Hide();
            Parent = parent;
            MouseMove += PreviewToolTip_MouseEnter;
            MouseLeave += PreviewToolTip_MouseLeave;
            AequitasInfo.MouseLeave += PreviewToolTip_MouseLeave;
            DemoInfo.MouseLeave += PreviewToolTip_MouseLeave;
            Bild.MouseLeave += PreviewToolTip_MouseLeave;
            
            foreach (Control c in Controls)
            {
                SetMouseReactions(c);
            }
            if (System.IO.File.Exists("Search_16.ico"))
                Bild.Cursor = new Cursor("Search_16.ico");
        }
        
        private void SetMouseReactions(Control c)
        {
            c.MouseEnter += PreviewToolTip_MouseEnter;
            c.MouseMove += PreviewToolTip_MouseEnter;
            //c.MouseLeave += PreviewToolTip_MouseLeave;
            foreach (Control c2 in c.Controls)
            {
                SetMouseReactions(c2);
            }
        }

        private void PreviewToolTip_VisibleChanged(object sender, EventArgs e)
        {
            if (Visible)
            {
                timerToolTipHide.Stop();
                timerToolTipHide.Interval = QuickLoad.Properties.Settings.Default.ToolTipTimeout;
                timerToolTipHide.Start();
            }
            else
            {
                //
            }
        }
        private void getDemoInfos()
        {
            string demo = m_Path;
            string Map = "Could not read map name!";
            StringCollection players = new StringCollection();
            long l1;
            long l2;
            CS_Plugin.IExport Demo_Data = new CS_Plugin.Export_Class();
            if (Demo_Data.Load_Demo(demo, true)) // true= nur Startzustand Laden  false= komplette Demo Laden
            {
                CS_Plugin.IStructures.Demo_Structure Demo_Info;
                CS_Plugin.IStructures.Game_Structure Game_Info;
                Demo_Info = Demo_Data.Get_Demo_Information(); //Hole informationen von der Demo
                Game_Info = Demo_Data.Get_Game_Information();  //Hole informationen zu dem Spiel
                if (Demo_Info.Map != null && Demo_Info.Map != string.Empty)
                    Map = "Map: " + Demo_Info.Map;
                else
                    Map = "Could not read map name!";
                if (Demo_Info.Players != null)
                {
                    for (l1 = 0; l1 <= Demo_Info.Players.Length - 1; l1++)
                    {
                        string[] tmpPlayerKeys;
                        long tmpPlayer_count;
                        tmpPlayer_count = Demo_Info.Players[l1].Keys.Count;
                        tmpPlayerKeys = new string[tmpPlayer_count];
                        Demo_Info.Players[l1].Keys.CopyTo(tmpPlayerKeys, 0);
                        for (l2 = 0; l2 <= tmpPlayerKeys.Length - 1; l2++)
                        {
                            players.Add(Demo_Info.Players[l1][tmpPlayerKeys[l2]] as string);
                        }
                    }
                }
                else
                    players.Add("Could not parse Playernames!");
            }
            else
            {
                players.Add("Demo has a not-valid format!");
                Map = "Could not read map name!";
            }
            AddItems(players,Map);
        }
        public void AddItems(StringCollection AddingItems,string Map)
        {
            if (this.DemoPlayerList.InvokeRequired)
            {
                AddItemCallback d = new AddItemCallback(AddItems);
                this.Invoke(d, new object[] { AddingItems, Map });
            }
            else
            {
                dMap.Text = "Map: " + Map;
                DemoPlayerList.Items.Clear();
                dPlayers.Text = "Players: (" + AddingItems.Count + ")";
                foreach (string s in AddingItems)
                {
                    DemoPlayerList.Items.Add(s,0);
                }
            }
        }
        

        public void ShowToolTip(string path,Point pos,Size size)
        {
            SuspendLayout();
            PreviewToolTip_MouseLeave(this, new EventArgs());
            m_Path = path;
            FileInfo f = new FileInfo(path);
            foreach (Control c in Controls)
            {
                c.Hide();
            }
            if(Helpers.getGameFileType(path) == GameFileType.Screenshot)
            {
                Bild.Image = Image.FromFile(path).GetThumbnailImage(size.Width, size.Height,new Image.GetThumbnailImageAbort(nixx),
                                                                                                      new IntPtr(0));
                Bild.Dock = DockStyle.Fill;
                Bild.BringToFront();
                Bild.Show();
            }
            else if(Helpers.getGameFileType(path) == GameFileType.Replay)
            {
                dSize.Text = lang.Size + ": " + Helpers.getFormattedKiloBytes(f.Length) + " kB";
                DemoInfo.Show();
                DemoInfo.BringToFront();
                DemoInfo.Location = new Point(0,0);
                DemoInfo.Dock = DockStyle.Fill;
                DemoPlayerList.Items.Clear();
                DemoPlayerList.Items.Add("Loading...");
                DemoParser = new Thread(new ThreadStart(getDemoInfos));
                DemoParser.Start();
            }
            else if (Helpers.getGameFileType(path) == GameFileType.Aequitas || Helpers.getGameFileType(path) == GameFileType.CompressedContent)
            {
                aSize.Text = lang.Size + ": " + Helpers.getFormattedKiloBytes(f.Length) + " kB";
                AequitasInfo.Location = new Point(0,0);
                aItems.Items.Clear();
                int ItemCount = 0;
                ZipInputStream s=null;
                try
                {
                     s = new ZipInputStream(File.OpenRead(path));
                    ZipEntry theEntry;
                    //bool fileFound = false;

                    while ((theEntry = s.GetNextEntry()) != null)
                    {
                        ItemCount++;
                        aItems.Items.Add(Path.GetFileName(theEntry.Name));
                    }
                }
                catch
                {
                }
                finally
                {
                    if(s != null)
                        s.Close();
                }
                aItemCount.Text = ItemCount.ToString() + " Item(s)";
                AequitasInfo.Show();
                AequitasInfo.BringToFront();
                AequitasInfo.Dock = DockStyle.Fill;
            }
            else 
                return;
            this.Size = size;
            Left = pos.X;
            Top = pos.Y;
            if (Top + Height + 26 > Parent.Height)
                Top = (Parent.Height - Height) - 26;
            ResumeLayout();
            Show();
            BringToFront();
        }
        private bool nixx()
        {
            return false;
        }
        private void timerToolTipHide_Tick(object sender, EventArgs e)
        {
            Hide();
            timerToolTipHide.Stop();
            if(DemoParser != null && DemoParser.ThreadState == ThreadState.Running)
                DemoParser.Abort();
        }

        private void PreviewToolTip_MouseEnter(object sender, EventArgs e)
        {
            timerToolTipHide.Stop();
        }

        private void PreviewToolTip_MouseLeave(object sender, EventArgs e)
        {
            timerToolTipHide.Stop();
            timerToolTipHide.Start();
        }

        private void Bild_Click(object sender, EventArgs e)
        {
            ImageClickedEventArgs ev = new ImageClickedEventArgs();
            ev.ImagePath = m_Path;
            if (ImageClicked != null)
                ImageClicked(sender, ev);
            //timerToolTipHide_Tick(sender, e);
        }
    }
    public class ImageClickedEventArgs : EventArgs
    {
        public string ImagePath;
    }
}
